<?php

require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/interfaces/IAccountAccessDao.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/interfaces/IConnectionAccessDao.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/factory/ConnectionFactory.php';
error_reporting (E_ALL ^ E_NOTICE);
	
$debug = $_SESSION['debug'];
  class MySqlAccountAccessDao implements IAccountAccessDao {
  
 	 	/**
		 * Constructor
		 */
	 public function __construct () {}
	 
  	 
    /*
	**************************CREATE ACCOUNT*****************************
	*/
     public function createAccount ($account) {
		 $GLOBALS['debug']->log("entro al dao account");	
		$type = $account->getType();
		$description = $account->getDescription();
		$name = $account->getName();
		 if($type=="a")
		 {
		    $type="ACTIVO";
		 }
		  if($type=="i")
		 {
		    $type="INGRESO";
		 }
		 if($type=="e")
		 {
		    $type="EGRESO";
		  }
		  if($type=="p")
		 {
		    $type="PASIVO";
		  }   
		
		$connection = ConnectionFactory::getMySqlConnectionAccessDao();
		$conn=$connection->openConnection();	
		 
		 $query = "INSERT INTO ACCOUNT(type,name,description)
		           VALUES ('$type', '$name', '$description')";
	 	 $GLOBALS['debug']->log( $query,"query insert account");
			 
	    $sentence=mysql_query($query,$conn);
	       if ($sentence){
			   	 $GLOBALS['debug']->log("salio del dao account exitosamente");
			    return true;
			   }
		$GLOBALS['debug']->log("salio del dao account  NO exitosamente");	   
		  return false;
	 
	 }

	/*
	************************CREATE ACCOUNTING_ENTRY***************************
	*/
	  public function createAccountingEntry ($accountingEntry) {
		
		$GLOBALS['debug']->log("entro el dao accounting");
	    $GLOBALS['debug']->log($dates,"fecha que llega al dao");
		$fech=date("Y-m-d",time());
		 $connection = ConnectionFactory::getMySqlConnectionAccessDao();
	     $GLOBALS['debug']->log("salio fabrica de coneccion");	
		 $conn=$connection->openConnection();
		 $GLOBALS['debug']->log("abrio coneccion");	
		 
		 	$query = "INSERT INTO ACCOUNTING_ENTRY(date) VALUES ('$fech')";
	
		  $sentence=mysql_query($query,$conn);
	       if ($sentence){
			    $GLOBALS['debug']->log("salio del dao accounting exitosamete");
			    return true;
			   }
			  $GLOBALS['debug']->log("salio del dao accounting  NO exitosamete");
		  return false;
	 
	 }
	
	/*
	***************CREATE BOOKKEEPING*************************
	*/
	
	  public function createBookkeeping ($bookkeeping) {
		 $GLOBALS['debug']->log("entro al dao bookkeeping");
		$debit=$bookkeeping->getDebit();
		$havings=$bookkeeping->getHavings();
		$fkLineBillDetail=$bookkeeping->getFkLineBillDetail();
		$fkNumberBillBillDetail=$bookkeeping->getFkNumberBillBillDetail();
		$fkControlNumberBillBillDetail=$bookkeeping->getFkControlNumberBillBillDetail();
		$fkCodeProductInventoryBillDetail=$bookkeeping->getFkCodeProductInventoryBillDetail();
		$fkRifBranchInventoryBillDetail=$bookkeeping->getFkRifBranchInventoryBillDetail();
		$fkIdPurchase=$bookkeeping->getFkIdPurchase();
		$fkRifSupplier=$bookkeeping->getFkRifSupplier();
		$fkCodeProductInventory=$bookkeeping->getFkCodeProductInventory();
		$fkRifBranchInventory=$bookkeeping->getFkRifBranchInventory();
		
		$connection = ConnectionFactory::getMySqlConnectionAccessDao();
		$conn=$connection->openConnection();
				 	
		$GLOBALS['debug']->log("armado el constructor de bookkeeping");

		/*******************ID MAX ACCOUNTI*********************************/	 
		 $query_account = "SELECT MAX(pk_number) as pknumber
				   FROM ACCOUNT";
	    $GLOBALS['debug']->log($query_account,"QUERY MAX ID ACCOUNT");
	    $sentences=mysql_query($query_account,$conn);
	    $GLOBALS['debug']->log($sentences,"resultado query");
		
		$row= mysql_fetch_array($sentences);
		$idMaxAccount= $row['pknumber'];
	    $GLOBALS['debug']->log($idMaxAccount,"pk number account");
		
		/*******************ID MAX ACCOUNTINGENTRY*************************/
		
		  $query_accountingEntry = "SELECT MAX(pk_number) as pknumber
				   FROM ACCOUNTING_ENTRY";
	    $GLOBALS['debug']->log($query_accountingEntry,"QUERY MAX ID ACCOUNT");
	    $sent=mysql_query($query_accountingEntry,$conn);
	    $GLOBALS['debug']->log($sent,"resultado query"); 
		
		$row= mysql_fetch_array($sent);
		$idMaxAccountingEntry= $row['pknumber'];
	    $GLOBALS['debug']->log($idMaxAccountingEntry,"pk number accountingEntry");
		
		
		/******************EJECUCION DEL DAO BOOKKEEPING*******************/
			$GLOBALS['debug']->log($debit,"debe");
			$GLOBALS['debug']->log($havings,"haber");
			
				 $query_bookkeeping = "INSERT INTO bookkeeping(debit,havings,pk_fk_number_accounting_entry, pk_fk_number_account,fk_line_bill_detail,fk_number_bill_bill_detail,fk_control_number_bill_bill_detail, fk_code_product_inventory_bill_detail,fk_rif_branch_inventory_bill_detail,fk_id_purchase,fk_rif_supplier_purchase, fk_code_product_inventory_purchase,fk_rif_branch_inventory_purchase) VALUES ($debit,$havings,$idMaxAccountingEntry, $idMaxAccount, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)";
			 
			 	$GLOBALS['debug']->log($query_bookkeeping,"QUERY BOOKKEEPING");
			 
				 $sentence=mysql_query($query_bookkeeping,$conn);
				   
				 $GLOBALS['debug']->log($sentence,"SENTENCE");  
				   
				   if ($sentence){
						$GLOBALS['debug']->log("salio del dao bookkeeping exitosamente");
						return true;
					   }
						$GLOBALS['debug']->log("salio del dao bookkeeping NO exitosamente");
				  return false;
		   
     }
	
	 
	 
  
  }


?>